package io.nutz.nutzsite.module.exam.services;

import io.nutz.nutzsite.common.base.Service;
import io.nutz.nutzsite.module.exam.models.Qdb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
/**
 * 题库 服务层实现
 * 
 * @author lrw
 * @date 2020-04-08
 */
@IocBean(args = {"refer:dao"})
public class QdbService extends Service<Qdb> {
	public QdbService(Dao dao) {
		super(dao);
	}

    /**
     * 对象转 题库 树
     *
     * @param list 题库列表
     * @return
     */
    //@Override
    public List<Map<String, Object>> getTrees(List<Qdb> list) {
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        for (Qdb data : list) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("id", data.getId());
            dataMap.put("pId", data.getPid());
            dataMap.put("name", data.getName());
            dataMap.put("title", data.getName());
            dataMap.put("checked", false);
            trees.add(dataMap);
        }
        return trees;
    }

    /**
     * 查询数据树
     * @param parentId
     * @param name
     * @return
     */
    //@Override
    public List<Map<String, Object>> selectTree(String parentId, String name) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(name)) {
            //cnd.and("name", "like", "%" + name + "%");
        }
        if (Strings.isNotBlank(parentId)) {
            cnd.and("pid", "=", parentId);
        }
        //cnd.and("status", "=", false).and("del_flag", "=", false);
        List<Qdb> list = this.query(cnd);
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        trees = getTrees(list);
        return trees;
    }
    public boolean checkQdbNameUnique(String id,String parentId,String menuName) {
        Cnd cnd =Cnd.NEW();
        if(Strings.isNotBlank(id)){
            cnd.and("id","!=",id);
        }
        if(Strings.isNotBlank(parentId)){
            cnd.and("pid","=",parentId);
        }
        if(Strings.isNotBlank(menuName)){
            cnd.and("name", "=", menuName);
        }
        List<Qdb> list = this.query(cnd);
        if (Lang.isEmpty(list)) {
            return true;
        }
        return false;
    }
}
